package com.jlsoft.config;


import com.jlsoft.config.handler.BrowerAuthenticationFailHandler;
import com.jlsoft.config.handler.BrowerAuthenticationSuccessHandler;
import com.jlsoft.config.handler.BrowerLogoutSuccesHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.UsesSunMisc;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
@SuppressWarnings("all")
public class BrowerSecurityConfig extends WebSecurityConfigurerAdapter {

//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
//    }

    /**
     * 权限配置动态配置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                .antMatchers("/login","/captcha","/resources/**").permitAll()
                .anyRequest().access("@rbacService.hasPermission(request, authentication)")
                //.authenticated()
            .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/authentication/form")
                .successHandler(successHandler)
                .failureHandler(failHandler)
            .and()
                .rememberMe()
                .tokenRepository(persistentTokenRepository())
                .tokenValiditySeconds(3600)
                .userDetailsService(userDetailsService)
            .and()
                .logout()
                .logoutUrl("/signOut")
                .logoutSuccessHandler(logoutSuccesHandler)
                .deleteCookies("JSESSIONID")
                .permitAll()
            .and()
                .httpBasic().disable()
                .csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //解决静态资源被拦截的问题
        web.ignoring().antMatchers(new String[]{"/plugins/**", "/style/**"});
    }

    @Autowired
    private DataSource dataSource;

    @Autowired
    private BrowerLogoutSuccesHandler logoutSuccesHandler;

    @Autowired
    private CustomUserDetailService userDetailsService;

    @Autowired
    private BrowerAuthenticationSuccessHandler successHandler;

    @Autowired
    private BrowerAuthenticationFailHandler failHandler;

    /**
     * 记住我 token实例
     * @return
     */
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        //tokenRepository.setCreateTableOnStartup(true);
        return tokenRepository;
    }
}
